/*
 * @(#)FeedBO.java $version 2011. 10. 14
 * 
 * Copyright 2011 NHN Corp. All rights Reserved.
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.hangame.mbl.bo.feed;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections15.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hangame.mbl.constant.CommonConst;
import com.hangame.mbl.dao.feed.FeedDAO;
import com.hangame.mbl.model.Feed;
import com.hangame.mbl.model.HspUserInfo;
import com.hangame.mbl.util.DateUtil;
import com.hangame.mbl.util.HspUserInfoUtil;
import com.nhncorp.lucy.web.metadata.Inject;

// TODO: Auto-generated Javadoc
/**
 * The Class FeedBO.
 *
 * @author NHN
 */
public class FeedBO {

	/** The LOG. */
	private final static Log LOG = LogFactory.getLog(FeedBO.class);

	/** The feed dao. */
	private FeedDAO feedDAO = null;
	
	/** The date util. */
	private DateUtil dateUtil = new DateUtil();

	/**
	 * Sets the feed dao.
	 * 
	 * @param feedDAO the new feed dao
	 */
	@Inject(beanId = "feedDAO")
	public void setFeedDAO(FeedDAO feedDAO) {
		this.feedDAO = feedDAO;
	}

	/**
	 * Gets the feed list.
	 * 
	 * @param gameNo the game id
	 * @param offset the offset
	 * @param count the count
	 * @param userId the user id
	 * @param isMyFeed the is my feed
	 * @return the feed list
	 */
	public List<Feed> getFeedList(int gameNo, int offset, int count, String userId, String isMyFeed) {
		List<Feed> feedList = new ArrayList<Feed>();
		try {
			feedList = feedDAO.selectFeedList(gameNo, offset, count, userId, isMyFeed);

			if (feedList != null) {
				for (int i = 0; i < feedList.size(); i++) {
					String registDateStr = dateUtil.getCommentDateType(feedList.get(i).getRegistDate());
					feedList.get(i).setRegistDateStr(registDateStr);
					String content = feedList.get(i).getContent();
					if (StringUtils.isNotEmpty(content)) {
						feedList.get(i).setContent(content.replace("\n", "<br>"));
					}
				}
			}

			return getHspUserInfo(feedList);
		} catch (Exception e) {
			LOG.error(e, e);
			return new ArrayList<Feed>();
		}
	}

	/**
	 * Gets the feed list count.
	 * 
	 * @param gameNo the game no
	 * @param userId the user id
	 * @param isMyFeed the is my feed
	 * @return the feed list count
	 */
	public int getFeedListCount(int gameNo, String userId, String isMyFeed) {
		try {
			return feedDAO.selectFeedListCount(gameNo, userId, isMyFeed);
		} catch (Exception e) {
			LOG.error(e, e);
			return 0;
		}
	}

	/**
	 * Write feed.
	 * 
	 * @param userId the user id
	 * @param gameNo the game no
	 * @param content the content
	 * @param attachFile the attach file
	 * @return the int
	 */
	public int writeFeed(String userId, int gameNo, String content, String attachFile) {
		int result = 0;
		try {
			String insertContent = formateContent(content);
			
			
			result = feedDAO.insertFeed(userId, gameNo, insertContent, attachFile);
			return result;
		} catch (Exception e) {
			LOG.error(e, e);
			return -1;
		}
	}

	/**
	 * Delete feed.
	 *
	 * @param feedNo the feed no
	 * @param userId the user id
	 * @return the int
	 */
	public int deleteFeed(int feedNo, String userId) {
		int result = 0;
		try {
			result = feedDAO.deleteFeed(feedNo, userId);
			return result;
		} catch (Exception e) {
			LOG.error(e, e);
			return -1;
		}
	}

	/**
	 * Gets the hsp user info.
	 * 
	 * @param feedList the feed list
	 * @return the hsp user info
	 */
	public List<Feed> getHspUserInfo(List<Feed> feedList) {
		if (feedList == null) {
			return feedList;
		}

		Map<String, String> userIdMap = new HashedMap<String, String>();
		for (Feed feed : feedList) {
			String userId = userIdMap.get(feed.getUserId());
			if (userId == null) {
				userIdMap.put(feed.getUserId(), feed.getUserId());
			}
		}

		Vector<String> userIdVector = new Vector<String>();
		List<String> userIdList = new ArrayList<String>(userIdMap.values());

		for (String userId : userIdList) {
			userIdVector.add(userId);
		}

		Map<String, HspUserInfo> hspUserInfoMap = HspUserInfoUtil.getHspUserInfo(userIdMap);

		if (hspUserInfoMap != null) {
			for (int i = 0; i < feedList.size(); i++) {
				HspUserInfo hspUserInfo = hspUserInfoMap.get(feedList.get(i).getUserId());
				if (hspUserInfo != null) {
					feedList.get(i).setUserNickName(hspUserInfo.getNickName());
					feedList.get(i).setUserProfileUrl(CommonConst.MPHOTO_PREFIX_URL + hspUserInfo.getsNo() + CommonConst.MPHOTO_SUFFIX_URL);
				} else {
					feedList.get(i).setUserNickName("");
					feedList.get(i).setUserProfileUrl(CommonConst.MPHOTO_PREFIX_URL + 0 + CommonConst.MPHOTO_SUFFIX_URL);
				}
			}
		}
		LOG.debug(feedList);
		return feedList;
	}
	
	/**
	 * Formate content.
	 * 
	 * @param content the content
	 * @return the string
	 */
	private String formateContent(String content) {
        Pattern pattern = Pattern.compile("(\r\n)\\1{1,}");   
        Matcher matcher = pattern.matcher(content);   
		
        return matcher.replaceAll("\n");
	}

}
